Skip to content

WebP: forward-only streaming decode of lossy stills - #21

Merged
sdcb merged 3 commits into
mainfrom
streaming/webp-decode
Nov 23, 2026
Merged

sdcb merged 3 commits into
mainfrom
streaming/webp-decode

Conversation

@sdcb

@sdcb sdcb commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Unit 9 of the streaming batch: forward-only streaming decode of WebP lossy stills, on the Abstractions foundation (84b58f2).

What changes

  • WebpImageDecoder implements IStreamingImageDecoder; Registration.CreateRowReader = CreateRowReader (concrete method group). New WebpRowReader : StreamingRowReader.
  • Simple VP8 and VP8X + VP8 (+ ALPH) at full size stream. The header reaches the VP8 chunk's first 10 bytes, so ICCP / EXIF / ALPH / unknown chunks before the image are buffered with it. The first partition (at most 512 KiB) is copied whole before the first row.
    • One token partition: macroblock rows are decoded as token bytes arrive. Memory is O(width) (row caches, band of 24 RGBA rows, and an input window of about two macroblock rows).
    • Several partitions: row y reads partition y mod n and the partitions are stored one after another, so the VP8 chunk is buffered first (O(compressed chunk) + O(width)). Rows then come as asked; IsStreaming stays true.
    • ALPH: the plane is decoded whole with the header (width × height bytes) and applied per row exactly as the span path applies it.
  • VP8L, animation and non-full DecodeOptions wait for the whole stream and use TryDecodeInto (IsStreaming == false); Info is then exactly TryReadInfo's.
  • Vp8Decoder.DecodeFrame is split into a resumable FrameState struct (Create / ReconstructRow / FinishRow / Save- and RestoreContexts). The span path runs the same code and still allocates nothing (WebpAllocationTests).
  • Vp8BoolDecoder gains a resume constructor (state + the next unread byte), Remaining, and Padded (set in the cold tail refill when it feeds zeros past its data).
    • A streamed row is decoded over the buffered window. If it read past a window that ends before the partition does, its contexts are restored and it is decoded again once more bytes arrive; the next attempt waits for twice the window.
    • At the partition's true end, zero padding is the stream's own (RFC 6386 §7.3), exactly as in the span decoder. So rows equal TryDecodeInto, and therefore libwebp 1.6.0.
  • Container check before the last row: the image's last row is held back until the container has been walked to the RIFF end with WebpContainer's rules. A file the span decoder refuses (truncated, bad trailing chunk, duplicates) therefore throws InvalidDataException here too.
  • Documented differences from TryDecodeInto:
    • Info lacks chunks stored after the image (EXIF orientation, ICC, a late VP8X alpha flag).
    • Two layouts the span decoder accepts are refused with InvalidDataException, because they change the image after its pixels: an ALPH chunk after the VP8 chunk, and an animated VP8X after it.
  • Docs and API: package README section; PublicAPI.Unshipped.txt lists WebpImageDecoder.CreateRowReader.
  • Benchmark: ab / quick time a Streaming case (a MemoryStream, 16 rows per read, checked equal to DecodeTo before timing) and show streaming ÷ current.

Tests (WebpStreamingDecodeTests, 87 cases)

  • Every fixture in Fixtures/webp plus ImageMagick-encoded and our-encoder inputs, × 10 PixelFormats (incl. premultiplied, Bgra, Rgb, Gray, 16-bit), × feeds (one byte, random chunks, async-only, whole), via StreamingAssert.RowsEqualDecodeInto. IsStreaming is asserted true for lossy (incl. partitions 2/4/8, lossy+alpha, VP8X) and false for lossless and animated input.
  • Truncation at ~100 cut points: TryDecodeInto false ⇔ streaming InvalidDataException.
  • Shortened token partitions with consistent chunk / RIFF sizes: the span decoder's zero-padding tolerance is matched.
  • Late ALPH is refused.
  • Info equals TryReadInfo for chunks before the image.
  • First 16 rows read < 1/4 of a 512×512 file.
  • 8192×8192 single-partition lossy: < 1 MiB managed allocation (measured 70 KB) and a first-band read under 1/8 of the file.
  • E2E: lossy-alpha fixture (in webp-oracle-golden.tsv) through ReadRowsAsync from an async-only ChunkedStream and an async FileStream equals TryDecodeInto.
  • Streaming fuzz (mutations, truncations, appended chunks), compared to TryDecodeInto.
  • Each check was confirmed to catch a deliberate bug: no context rollback (45 fail), no starvation detection (47 fail), no trailer check (3 fail).

Runs:

  • Full suite: 7127 passed, 0 failed.
  • All Webp classes with REQUIRE_ORACLES=1 (dwebp / anim_dump / webpinfo 1.6.0): 622 passed (2 skips are the golden-regeneration switches).
  • DOTNET_EnableAVX2=0 and DOTNET_EnableHWIntrinsic=0: 622 passed each.
  • Soak: WEBP_STREAM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~WebpStreamingDecode" (method Streaming_fuzz): passed, about 20 s.

Benchmark (AMD Ryzen 7 5800X, Windows 11 26200, .NET 10.0.15, AVX2, baseline 533b03e)

The machine was shared with ~47 dotnet processes from the parallel batch, so single runs move by ±8%. The figures are three quick --rounds 40 runs plus one BDN ab run:

input current ÷ baseline (span) streaming ÷ current
lossy-opaque-1080p 0.98–1.00× (ab 0.98×) 1.03–1.05× (ab 1.09×)
lossy-alpha-1080p 0.95–0.99× (ab 1.03×) 1.04–1.10× (ab 1.01×)

Span decode does not regress. A first version checked starvation inside the macroblock loop and measured 8% slower; it now checks once per row (second commit). Streaming is about 1.05× the span decode.

Needs outside this unit

  • AGENTS.md soak list: add WEBP_STREAM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~WebpStreamingDecode" after touching the VP8 bool decoder, Vp8Decoder.FrameState or WebpRowReader.
  • The wasm smoke test was not run. The facade is untouched; this only adds CreateRowReader to the registration, bound by concrete method group.

🤖 Generated with Claude Code

sdcb and others added 3 commits November 23, 2026 19:51
WebpImageDecoder implements IStreamingImageDecoder. A lossy still (simple VP8,
or VP8X with VP8 and optional ALPH) decodes macroblock row by macroblock row as
the VP8 chunk arrives: with one token partition from a window of about two
macroblock rows of input (O(width) memory), with several from the VP8 chunk
buffered whole. The ALPH plane is decoded whole with the header. Lossless,
animated input and non-full options take the buffered fallback.

Vp8Decoder.DecodeFrame is split into a resumable FrameState (ReconstructRow /
FinishRow); the span path runs the same code and stays allocation-free. The
bool decoder can resume from saved state and reports reading past its window,
so a starved row is rolled back and decoded again once its bytes arrive. The
last row waits until the container is checked to the RIFF end, so files the
span decoder refuses fail here too.

Tests: every fixture x 10 formats x 4 feeds equal TryDecodeInto; truncation,
short partitions, late ALPH, 8192x8192 bounded memory, prefix reads, async
end-to-end, streaming fuzz (WEBP_STREAM_FUZZ_ITERATIONS, soaked at 300000).
The Webp benchmark times a Streaming case beside Current.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The per-macroblock Padded test in ReconstructRow made the span decode of
lossy-opaque-1080p 8% slower (quick A/B, Ryzen 7 5800X); the streaming
reader now tests it after the row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a macroblock row's output fits the rows asked for, the sink writes it
to the destination instead of the band, saving a copy per row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sdcb
sdcb merged commit b2c9838 into main Nov 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant